import swiper from "@/components/Swiper"
import { gettoday } from "@/utils/util.js"
import { getTide, getNotice, getWarn, getWarnDetail, getNoticeDetail, getQuick } from "@/api/home.js"
export default {
    name: "home",
    data() {
        return {
            tckState: false,//弹出框状态
            tabstate: false,//表格状态
            today: "",
            tideList: [],//潮汐信息
            day: 0,
            noticeList: [],//公告信息
            warnList: {},//预警信息
            detail: {},//详细信息
            userinfo: null,//用户信息
            userNav:[],
            quickList: [],//船舶动态
            param: {
                vesselName: "",
                startTime: "",
                endTime: "",
            },
            btnstate: true,
            toTop: false,
            lbstate: false,
            tkstate: false,
            screenWidth: document.body.clientWidth,
        }
    },
    mounted() {
        const that = this;
        window.onresize = () => {
            return (() => {
                window.screenWidth = document.body.clientWidth
                that.screenWidth = window.screenWidth
            })()
        }
        document.onkeydown = function (e) {
            let ev = document.all ? window.event : e
            if (ev.keyCode === 13) {
                if (that.btnstate) {
                    that.btnstate = false
                    that.GetQuick();
                }
                let time = setTimeout(() => {
                    that.btnstate = true;
                    clearTimeout(time)
                }, 1000);

            }
        }
    },
    created() {
        if (this.$cookies.isKey("userinfo")) {
            this.userinfo = this.$cookies.get("userinfo")
        }
        else if (typeof (this.$session.get("userinfo")) !== "undefined") {
            this.userinfo = this.$session.get("userinfo")
        }
        if(typeof (this.$session.get("userNav")) !== "undefined"){
            this.userNav = this.$session.get("userNav")
        }
        // this.$cookies.get("userinfo")
        this.today = gettoday(new Date())
        this.init();
    },

    components: { swiper },
    methods: {
        init() {
            this.GetTide();
            this.GetNotice();
            this.GetWarn();
            this.GetQuick();
        },
        //切换语言
        editLang(lang) {
            this.$i18n.locale = lang;
        },
        //关闭弹出框
        closetck() {
            this.tckState = false
        },
        //显示弹出框
        showtck(type) {
            if (type == "notice") {
                this.GetNoticeDetail()
            } else if (type == "warn") {
                this.GetWarnDetail()
            }
            this.tckState = true
        },
        GetTide() {
            let that = this;
            getTide().then(res => {
                if (res.data.code === "0000") {
                    that.tideList = res.data.data.HL;
                }
            })
        },
        GetQuick() {
            let that = this;
            // var reg = new RegExp('-', "g")
            let param = {
                "DEPEND_PLANTIME_START": this.param.startTime,
                "DEPEND_PLANTIME_END": this.param.endTime,
                "VESSEL_NAMEC": this.param.vesselName,
                "VESSEL_NAMEE": "",
                "SHIP_REGISTER": "",
                "VESSEL_IMONO": "",
                "AUDIT_STATE": "Y"
            }
            getQuick(param).then(res => {
                if (res.data.code === "0000") {
                    that.quickList = res.data.data.slice(0, 9);
                }
            })
        },
        GetNotice() {
            let that = this;
            getNotice().then(res => {
                if (res.data.code === "0000") {
                    that.noticeList = res.data.data.ANNO.slice(0, 4);
                }
            })
        },
        GetWarn() {
            let that = this;
            getWarn().then(res => {
                if (res.data.code === "0000") {
                    that.warnList = res.data.data.WARN.slice(0, 4);
                }
            })
        },
        GetWarnDetail() {
            let that = this;
            getWarnDetail().then(res => {
                if (res.data.code === "0000") {
                    that.detail = res.data.data;
                }
            })
        },
        GetNoticeDetail() {
            let that = this;
            getNoticeDetail().then(res => {
                if (res.data.code === "0000") {
                    that.detail = res.data.data;
                }
            })
        },
        setDay(day) {
            let date = new Date();
            date.setDate(date.getDate() + day);
            this.today = gettoday(date);
            this.day = day;
        },
        ToLogin() {
            if (this.userinfo === null) {
                this.$router.push({ path: 'login' })
            }
        },
        SignOut() {
            this.tkstate = false
            this.$cookies.remove("userinfo")
            this.$session.remove("userinfo")
            this.$session.remove("userNav")
            this.userinfo = null;
            this.userNav=[];

        },
        showliebiao() {
            this.lbstate = true
        },
        hiddenliebiao() {
            this.lbstate = false
        },
        showtk() {
            this.tkstate = true
        },
        hiddentk() {
            this.tkstate = false
        },
        href(url) {
            if (this.userinfo != null) {
                // window.open(url)
                window.top.location.href = url;
            }
            else {
                this.$router.push({ name: 'login', params: { url: url } })
            }
        }

    },
};